<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="roleDialogVisible=true, tableTitle=true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roleList" border="">
              <el-table-column align="center" label="序号" width="120" type="index" />
              <el-table-column align="center" label="角色名称" width="240" prop="name" />
              <el-table-column align="center" label="描述" prop="description" />
              <el-table-column align="center" label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="assignPerm(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(scope.row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteRole(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :current-page="roleListPage.page"
                :page-size="roleListPage.pagesize"
                :total="roleListPage.total"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="formDate.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formDate.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formDate.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formDate.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>
      </el-card>
    </div>
    <!-- 编辑新增角色对话框 -->
    <el-dialog
      :title="tableTitle ? '新增角色':'编辑角色'"
      :visible.sync="roleDialogVisible"
      width="30%"
      :before-close="closeEditRoleFrom"
    >
      <el-form ref="refRoleForm" :rules="addRoleRulse" :model="roleForm" label-width="80px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeEditRoleFrom">取 消</el-button>
        <el-button type="primary" @click="sendEditRole">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 分配权限对话框 -->
    <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPremCancel">
      <el-tree
        ref="permTree"
        node-key="id"
        :data="parmData"
        :props="defaultProps"
        default-expand-all
        :default-checked-keys="selectCheck"
        show-checkbox
        check-strictly
      />
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnPremOk">确定</el-button>
          <el-button size="small" @click="btnPremCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, getCompanyList, deleteRole, getRoleDeta, editRole, addRole, assignPerm } from '@/api/setting'
import { getPermissionList } from '@/api/permission'
import { tranListToTreeData } from '@/utils/index'
import { mapGetters } from 'vuex'
export default {
  name: 'RoleIdex',
  data() {
    return {
      // 获取员工列表配置项
      roleListPage: {
        page: 1,
        pagesize: 5,
        total: 0
      },
      // 员工列表信息
      roleList: [],
      // 企业信息
      formDate: {
      },
      // 修改/新增角色对话框显示隐藏
      roleDialogVisible: false,
      // 角色信息列表
      roleForm: {},
      addRoleRulse: {
        name: [{ required: true, message: '名称不能为空' }]
      },
      tableTitle: false,
      // 控制分配权限的对话框
      showPermDialog: false,
      // 权限树数据
      parmData: [],
      defaultProps: {
        label: 'name'
      },
      // 当前树选中项id
      userId: null,
      // 当前勾选树节点id
      selectCheck: []

    }
  },
  computed: {
    ...mapGetters(['companyId'])
    // newIsEdit:{
    //   if(roleForm =null){
    //     return
    //   }

    // }
  },
  created() {
    this.loadGetRoleList()
    this.lodaGetCompanyList()
  },
  methods: {
    // 获取员工列表信息
    async loadGetRoleList() {
      const data = await getRoleList(this.roleListPage)
      console.log(data)
      this.roleList = data.rows
      this.roleListPage.total = data.total
    },
    // 分页点击触发事件
    handleCurrentChange(newPage) {
      this.roleListPage.page = newPage
      this.loadGetRoleList()
    },
    // 根据id查询企业信息
    async lodaGetCompanyList() {
      this.formDate = await getCompanyList(this.companyId)
      console.log('企业信息', this.formDate)
    },
    // 根据id删除角色
    async deleteRole(id) {
      try {
        await this.$confirm('确认删除角色吗')
        await deleteRole(id)
        this.loadGetRoleList()
        this.$message.success('删除角色成功')
      } catch (err) {
        alert('删除失败')
      }
    },
    // 根据角色id获取当前角色信息
    async editRole(id) {
      this.roleForm = await getRoleDeta(id)
      this.tableTitle = false
      this.roleDialogVisible = true
    },
    // 发送修改请求
    async sendEditRole() {
      try {
        this.$refs.refRoleForm.validate()
        if (this.roleForm.id) {
          await editRole(this.roleForm)
          this.$message.success('修改角色信息成功')
        } else {
          await addRole(this.roleForm)
        }
      } catch (err) {
        alert('错误')
      }
      this.closeEditRoleFrom()
      this.loadGetRoleList()
    },
    // 关闭修改用户表单重置表单
    closeEditRoleFrom() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.roleDialogVisible = false
      this.$refs.refRoleForm.resetFields()
    },
    // 获取权限分配对话框数据
    async assignPerm(id) {
      this.parmData = tranListToTreeData(await getPermissionList(), '0')
      const { permIds } = await getRoleDeta(id)
      this.selectCheck = permIds
      this.showPermDialog = true
      this.userId = id
    },
    // 分配权限确认请求
    async btnPremOk() {
      await assignPerm({
        permIds: this.$refs.permTree.getCheckedKeys(),
        id: this.userId
      })
      this.$message.success('修改权限数据成功')
      this.showPermDialog = false
    },
    // 分配权限取消按钮
    btnPremCancel() {
      this.showPermDialog = false
      this.selectCheck = []
    }
    // 添加角色
  }
}
</script>

<style>

</style>
